﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Threading;
using System.ComponentModel;
using System.Reflection;

namespace NaviQuest_v2
{
    partial class NaviControl : Form
    {
        /*------------Misc Variables------------*/
        #region
        //Collect List Totals
        Int32 progVal = 0;
        Int32 fin_total = NaviControl_Collect_Lists_Total();
        #endregion

        /*--------------Primary Function--------------*/
        #region
        public string NaviControl_Internal_Loader()
        {
            //Before the main function starts report 
            //the final total from list total collection
            //and set the progress value at 1
            NaviControl_Update_Progressbar(fin_total, 1);

            NaviControl_Update_Log("text", 1, "Loading Quest List...");
            /* -------------- Quest -------------- */
            #region
            //Attempt to create a table to store quest information
            if (create_quest_table() == "table_created")
            {
                //If table creation was successful,
                //attempt to populate the previous
                //datatable with information
                if (populate_quest_table() == "quest_list_populated")
                {
                    //At this point the list has successfully loaded,
                    //the datatable has been created, we should set 
                    //the quest_list datasource to the populated datatable
                    this.Invoke(new MethodInvoker(delegate { quest_list.DataSource = quest_table; }));

                    //Since loading from DataSource doesn't have native
                    //column formatting lets format the cells
                    NaviControl_Format_Quests();

                    NaviControl_Update_Log("update", 0, "OK");
                }
                else
                {
                    //If the population failed send
                    //failure message
                    return "internal_loader_unexpected_error || quest_datatable_populate_failed";
                }
            }
            else
            {
                //If the table creation was unsuccessful,
                //send failure message
                return "internal_loader_unexpected_error || quest_datatable_creation_failed";
            }   
            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest Link List...");
            /* ------------ QuestLink ------------ */
            #region
            //Attempt to create the QuestLink table to store the info
            if (create_questlink_table() == "table_created")
            {
                //If table creation was successful,
                //attempt to populate the previous
                //datatable with information
                if (populate_questlink_table() == "questlink_list_populated")
                {
                    //If population was successful
                    //send the success message
                    NaviControl_Update_Log("update", 0, "OK");
                }
                else
                {
                    //If population failed
                    //send failure message
                    return "internal_loader_unexpected_error || questlink table populate failed";
                }
            }
            else
            {
                //If QuestLink table creation failed
                //send failure message
                return "internal_loader_unexpected_error || questlink table creation failed";
            }          
            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest Basic Info List...");
            /* -------------- Basic -------------- */
            #region
            //Attempt to create datatable to store basic info
            if (create_basic_table() == "table_created")
            {
                //If table creation was successful,
                //attempt to populate the previous
                //datatable with information
                if (populate_basic_table() == "basic_info_populated")
                {
                    //Table created and populate
                    //send success message
                    NaviControl_Update_Log("update", 0, "OK");
                }
                else
                {
                    return populate_basic_table();
                }
            }
            else
            {
                return "internal_loader_unexpected_error || basic_info_datatable_creation_failed";
            }
            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest NPC Info List...");
            /* --------------  NPC  -------------- */
            #region
            //Attempt to create a table to store npc information
            if (create_npc_table() == "table_created")
            {
                //If table creation was successful,
                //attempt to populate the previous
                //datatable with information
                if (populate_npc_table() == "npc_list_populated")
                {
                    //If population succeeded send
                    //success message
                    NaviControl_Update_Log("update", 0, "OK");
                }
                else
                {
                    //If the population failed send
                    //failure message
                    return "internal_loader_unexpected_error || npc_datatable_populate_failed";
                }
            }
            else
            {
                //If the table creation was unsuccessful,
                //send failure message
                return "internal_loader_unexpected_error || npc_datatable_creation_failed";
            }
            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest Monster Info List...");
            /* ------------- Monster ------------- */
            #region
            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest Objective Info List...");
            /* ------------- Objectives ------------- */
            #region

            /* ------------- Item ------------- */
            #region
            //Attempt to create a table to store objective information
            if (create_item_objective_table() == "table_created")
            {
                //If table creation was successful,
                //attempt to populate the previous
                //datatable with information
                if (populate_item_objective_table() == "item_objective_list_populated")
                {
                    //If population succeeded send
                    //success message
                    NaviControl_Update_Log("update", 0, "OK");
                }
                else
                {
                    NaviControl_Update_Log("update", 0, "FAIL");

                    //If the population failed send
                    //failure message
                    return "internal_loader_unexpected_error || item_objective_datatable_populate_failed";
                }
            }
            else
            {
                //If the table creation was unsuccessful,
                //send failure message
                return "internal_loader_unexpected_error || item_objective_datatable_creation_failed";
            }
            #endregion

            /* ------------- Monster ------------- */
            #region
            #endregion

            /* ------------- Skill ------------- */
            #region
            #endregion

            #endregion

            NaviControl_Update_Log("text", 1, "Loading Quest Reward Info List...");
            /* ------------- Rewards ------------- */
            #region


            #endregion

            //Report Success Message
            return "internal_loader_success";
        }
        #endregion

        /*--------------Sub Function's--------------*/
        #region
        //Create the Quest Table to store Search Info
        public string create_quest_table()
        {
            try
            {
                //Add quest_table columns, set datatypes
                quest_table.Columns.Add("id", typeof(Int32));
                quest_table.Columns.Add("name", typeof(String));
                quest_table.Columns.Add("lv", typeof(Int32));
                quest_table.Columns.Add("summary", typeof(String));

                //Return success message
                return "table_created";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "table_error";
            }
        }

        //Populate the previously created Quest Table with Search Info
        public string populate_quest_table()
        {
            //Define Variables
            int progVal = 0;

            //Update NaviControl Progres Bar
            NaviControl_Update_Progressbar(fin_total, 1);

            //Change Titlebar text to reflect action in-progress
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor -- Loading Quests..."; }));

            //While the reader is initialized and reading
            while (internal_quest_reader.Peek() != -1)
            {
                //We define the fields by a \t or tab delimiter in the Basic.txt
                string[] fieldData = internal_quest_reader.ReadLine().Split('\t');

                //Make sure it has  fields
                if (fieldData.Length == 4)
                {
                    DataRow row = quest_table.NewRow();
                    row["id"] = Int32.Parse(fieldData[0]);
                    row["name"] = fieldData[1];
                    row["lv"] = Int32.Parse(fieldData[2]);
                    row["summary"] = fieldData[3];
                    quest_table.Rows.Add(row);
                    NaviControl_Update_Progressbar(0, progVal++);
                    progVal++;
                }
                else
                {
                    MessageBox.Show(fieldData.Length.ToString());
                }
            }

            //Close and Dispose the Reader
            internal_quest_reader.Close();
            internal_quest_reader.Dispose();

            //Return Titlebar text to normal
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor"; }));

            //Since at this point the read has completed
            //send the success message
            return "quest_list_populated";
        }

        //Create the QuestLink Table to store NPC related Quest Data
        public string create_questlink_table()
        {
            try
            {
                //Add questlink_table columns, set datatypes
                questlink_table.Columns.Add("id", typeof(Int32));
                questlink_table.Columns.Add("npc_id", typeof(Int32));
                questlink_table.Columns.Add("flag_start", typeof(Int32));
                questlink_table.Columns.Add("flag_progress", typeof(Int32));
                questlink_table.Columns.Add("flag_end", typeof(Int32));

                //Return success message
                return "table_created";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "table_error";
            }
        }

        //Populate the previously created QuestLink Table with Info
        public string populate_questlink_table()
        {
            //Define Variables
            int progVal = 0;

            //Change Titlebar text to reflect action in-progress
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor -- Loading QuestLink Info..."; }));

            //While the reader is initialized and reading
            while (internal_questlink_reader.Peek() != -1)
            {
                //We define the fields by a \t or tab delimiter in the Basic.txt
                string[] fieldData = internal_questlink_reader.ReadLine().Split('\t');

                //Make sure it has  fields
                if (fieldData.Length == 5)
                {
                    DataRow row = questlink_table.NewRow();
                    row["id"] = Int32.Parse(fieldData[0]);
                    row["npc_id"] = Int32.Parse(fieldData[1]);
                    row["flag_start"] = Int32.Parse(fieldData[2]);
                    row["flag_progress"] = Int32.Parse(fieldData[3]);
                    row["flag_end"] = Int32.Parse(fieldData[4]);
                    questlink_table.Rows.Add(row);
                    NaviControl_Update_Progressbar(0, progVal++);
                    progVal++;
                }
                else
                {
                    return "internal_loader_questlink_unexpected_error";
                }
            }

            //Close and Dispose the Reader
            internal_questlink_reader.Close();
            internal_questlink_reader.Dispose();

            //Return Titlebar text to normal
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor"; }));

            //Since at this point the read has completed
            //send the success message
            return "questlink_list_populated";
        }

        //Create the Basic Info Table to store Quest Basic Info
        public string create_basic_table()
        {
            try
            {
                basic_table.Columns.Add("id", typeof(Int32));
                basic_table.Columns.Add("name", typeof(String));
                basic_table.Columns.Add("repeatable", typeof(Int32));
                basic_table.Columns.Add("time_limit_type", typeof(Int32));
                basic_table.Columns.Add("time_limit", typeof(Int32));
                basic_table.Columns.Add("limit_asura", typeof(Int32));
                basic_table.Columns.Add("limit_deva", typeof(Int32));
                basic_table.Columns.Add("limit_gaia", typeof(Int32));
                basic_table.Columns.Add("limit_fighter", typeof(Int32));
                basic_table.Columns.Add("limit_magician", typeof(Int32));
                basic_table.Columns.Add("limit_summoner", typeof(Int32));
                basic_table.Columns.Add("limit_hunter", typeof(Int32));
                basic_table.Columns.Add("type", typeof(String));
                basic_table.Columns.Add("value1", typeof(Int32));
                basic_table.Columns.Add("value2", typeof(Int32));
                basic_table.Columns.Add("value3", typeof(Int32));
                basic_table.Columns.Add("value4", typeof(Int32));
                basic_table.Columns.Add("value5", typeof(Int32));
                basic_table.Columns.Add("value6", typeof(Int32));
                basic_table.Columns.Add("value7", typeof(Int32));
                basic_table.Columns.Add("value8", typeof(Int32));
                basic_table.Columns.Add("value9", typeof(Int32));
                basic_table.Columns.Add("value10", typeof(Int32));
                basic_table.Columns.Add("value11", typeof(Int32));
                basic_table.Columns.Add("value12", typeof(Int32));
                basic_table.Columns.Add("default_reward_id", typeof(Int32));
                basic_table.Columns.Add("default_reward_level", typeof(Int32));
                basic_table.Columns.Add("default_reward_quantity", typeof(Int32));
                basic_table.Columns.Add("optional_reward_id1", typeof(Int32));
                basic_table.Columns.Add("optional_reward_level1", typeof(Int32));
                basic_table.Columns.Add("optional_reward_quantity1", typeof(Int32));
                basic_table.Columns.Add("optional_reward_id2", typeof(Int32));
                basic_table.Columns.Add("optional_reward_level2", typeof(Int32));
                basic_table.Columns.Add("optional_reward_quantity2", typeof(Int32));
                basic_table.Columns.Add("optional_reward_id3", typeof(Int32));
                basic_table.Columns.Add("optional_reward_level3", typeof(Int32));
                basic_table.Columns.Add("optional_reward_quantity3", typeof(Int32));
                basic_table.Columns.Add("optional_reward_id4", typeof(Int32));
                basic_table.Columns.Add("optional_reward_level4", typeof(Int32));
                basic_table.Columns.Add("optional_reward_quantity4", typeof(Int32));
                basic_table.Columns.Add("optional_reward_id5", typeof(Int32));
                basic_table.Columns.Add("optional_reward_level5", typeof(Int32));
                basic_table.Columns.Add("optional_reward_quantity5", typeof(Int32));
                basic_table.Columns.Add("script_start_text", typeof(String));
                basic_table.Columns.Add("script_end_text", typeof(String));
                basic_table.Columns.Add("start_desc", typeof(String));
                basic_table.Columns.Add("mid_desc", typeof(String));
                basic_table.Columns.Add("end_desc", typeof(String));
                return "table_created";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "table_error";
            }
        }

        //Populate the previously created Basic Info Table with Basic Info
        public string populate_basic_table() 
        {
            //Change Titlebar text to reflect action in-progress
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor -- Loading Basic Info..."; }));

            //While the reader is initialized and reading
            while (internal_basic_reader.Peek() != -1)
            {
                //We define the fields by a \t or tab delimiter in the Basic.txt
                string[] fieldData = internal_basic_reader.ReadLine().Split('\t');

                //Make sure it has 13 fields
                if (fieldData.Length == 48)
                {
                    //Create a new DataRow to store the Basic Info
                    try
                    {
                        DataRow row = basic_table.NewRow();
                        row["id"] = Int32.Parse(fieldData[0]);
                        row["name"] = fieldData[1];
                        row["repeatable"] = fieldData[2];
                        row["time_limit_type"] = fieldData[3];
                        row["time_limit"] = fieldData[4];
                        row["limit_asura"] = Int32.Parse(fieldData[5]);
                        row["limit_deva"] = Int32.Parse(fieldData[6]);
                        row["limit_gaia"] = Int32.Parse(fieldData[7]);
                        row["limit_fighter"] = Int32.Parse(fieldData[8]);
                        row["limit_magician"] = Int32.Parse(fieldData[9]);
                        row["limit_summoner"] = Int32.Parse(fieldData[10]);
                        row["limit_hunter"] = Int32.Parse(fieldData[11]);
                        row["type"] = (fieldData[12]);
                        row["value1"] = (fieldData[13]);
                        row["value2"] = (fieldData[14]);
                        row["value3"] = (fieldData[15]);
                        row["value4"] = (fieldData[16]);
                        row["value5"] = (fieldData[17]);
                        row["value6"] = (fieldData[18]);
                        row["value7"] = (fieldData[19]);
                        row["value8"] = (fieldData[20]);
                        row["value9"] = (fieldData[21]);
                        row["value10"] = (fieldData[22]);
                        row["value11"] = (fieldData[23]);
                        row["value12"] = (fieldData[24]);
                        row["default_reward_id"] = (fieldData[25]);
                        row["default_reward_level"] = (fieldData[26]);
                        row["default_reward_quantity"] = (fieldData[27]);
                        row["optional_reward_id1"] = (fieldData[28]);
                        row["optional_reward_level1"] = (fieldData[29]);
                        row["optional_reward_quantity1"] = (fieldData[30]);
                        row["optional_reward_id2"] = (fieldData[31]);
                        row["optional_reward_level2"] = (fieldData[32]);
                        row["optional_reward_quantity2"] = (fieldData[33]);
                        row["optional_reward_id3"] = (fieldData[34]);
                        row["optional_reward_level3"] = (fieldData[35]);
                        row["optional_reward_quantity3"] = (fieldData[36]);
                        row["optional_reward_id4"] = (fieldData[37]);
                        row["optional_reward_level4"] = (fieldData[38]);
                        row["optional_reward_quantity4"] = (fieldData[39]);
                        row["optional_reward_id5"] = (fieldData[40]);
                        row["optional_reward_level5"] = (fieldData[41]);
                        row["optional_reward_quantity5"] = (fieldData[42]);
                        row["script_start_text"] = (fieldData[43]);
                        row["script_end_text"] = (fieldData[44]);
                        row["start_desc"] = (fieldData[45]);
                        row["mid_desc"] = (fieldData[46]);
                        row["end_desc"] = (fieldData[47]);
                        //Add created DataRow to the BasicInfo Table
                        basic_table.Rows.Add(row);
                    }
                    catch
                    {
                        return "internal_list_loader_unexpected_error || record_entry_failed";
                    }
                    
                    //Update NaviControls Progress Bar accordingly
                    NaviControl_Update_Progressbar(0, progVal++);
                    progVal++;
                }
                else
                {
                    return "internal_list_loader_unexpected_error || column_count_incorrect";
                }
            }
            //Close and Dispose the Reader
            internal_basic_reader.Close();
            internal_basic_reader.Dispose();

            //Return Titlebar text to normal
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor"; }));

            return "basic_info_populated";
        }

        //Create the NPC Info table to store NPC Info
        public string create_npc_table()
        {
            try
            {
                //Add quest_table columns, set datatypes
                npc_table.Columns.Add("id", typeof(Int32));
                npc_table.Columns.Add("name", typeof(String));
                npc_table.Columns.Add("contact_script", typeof(String));
                npc_table.Columns.Add("x", typeof(Int32));
                npc_table.Columns.Add("y", typeof(Int32));

                //Return success message
                return "table_created";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "table_error";
            }
        }

        //Populate the previously created NPC Info Table with NPC Info
        public string populate_npc_table()
        {
            //Change Titlebar text to reflect action in-progress
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor -- Loading NPC Info..."; }));

            //While the reader is initialized and reading
            while (internal_npc_reader.Peek() != -1)
            {
                //We define the fields by a \t or tab delimiter in the Basic.txt
                string[] fieldData = internal_npc_reader.ReadLine().Split('\t');

                //Make sure it has  fields
                if (fieldData.Length == 5)
                {
                    DataRow row = npc_table.NewRow();
                    row["id"] = Int32.Parse(fieldData[0]);
                    row["name"] = fieldData[1];
                    row["contact_script"] = fieldData[2];
                    row["x"] = Int32.Parse(fieldData[3]);
                    row["y"] = Int32.Parse(fieldData[4]);
                    npc_table.Rows.Add(row);
                    NaviControl_Update_Progressbar(0, progVal++);
                    progVal++;
                }
                else
                {
                    return "internal_loader_npc_unexpected_error";
                }
            }

            //Close and Dispose the Reader
            internal_npc_reader.Close();
            internal_npc_reader.Dispose();

            //Return Titlebar text to normal
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor"; }));

            //Since at this point the read has completed
            //send the success message
            return "npc_list_populated";
        }

        //Create the Item Objective Table to store Item Objectives
        public string create_item_objective_table()
        {
            try
            {
                //Add quest_table columns, set datatypes
                item_objectives_table.Columns.Add("item_id", typeof(Int32));
                item_objectives_table.Columns.Add("item_name", typeof(String));
                item_objectives_table.Columns.Add("item_desc", typeof(String));
 
                //Return success message
                return "table_created";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return "table_error";
            }
        }

        //Populate the Item Objective Table
        public string populate_item_objective_table()
        {
            //Change Titlebar text to reflect action in-progress
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor -- Loading Item Objectives Info..."; }));

            //While the reader is initialized and reading
            while (internal_item_objective_reader.Peek() != -1)
            {
                //We define the fields by a \t or tab delimiter
                string[] fieldData = internal_item_objective_reader.ReadLine().Split('\t');

                //Make sure it has 3 fields
                if (fieldData.Length == 3)
                {
                    DataRow row = item_objectives_table.NewRow();

                    row["item_id"] = Int32.Parse(fieldData[0]);
                    row["item_name"] = fieldData[1];
                    row["item_desc"] = fieldData[2];               

                    item_objectives_table.Rows.Add(row);
                    NaviControl_Update_Progressbar(0, progVal++);
                    progVal++;
                }
                else
                {
                    return "internal_loader_item_unexpected_error";
                }
            }

            //Close and Dispose the Reader
            internal_item_objective_reader.Close();
            internal_item_objective_reader.Dispose();

            //Return Titlebar text to normal
            this.Invoke(new MethodInvoker(delegate { this.Text = "NaviQuest Editor"; }));

            //Since at this point the read has completed
            //send the success message
            return "item_objective_list_populated";
        }

        #endregion
    }
}
